/*
* @Author:  AnHao
* @Function:对原生应用跳转HTML5页面做相应操作
*/
$(function(){
	
	//判断是否为IOS或ANDROID原生应用   name:app的系统
	function contrast(name){
		if(name=="ios"||name=="android"){
			$(".ios_android").hide();
			console.log("yg");
			//存入cookie
			document.cookie="fromApp="+name+";path=/";
			interactive(name);
		}else{
			//读取cookie
			var appIdentify=getCookie("fromApp");
			if(appIdentify=="ios"||appIdentify=="android"){
				$(".ios_android").hide();
				console.log("cookie");
				interactive(appIdentify);
			}else{
				$(".white").hide();
				$(".yg_iosNav").hide();
			}
		}
	}
	
	var appIdentify=GetQueryString("fromApp");
	contrast(appIdentify);	
	console.log(appIdentify);
	
	//点评页面 输入内容的判断
	function filteremoji(str){
	    var ranges = [
	        '\ud83c[\udf00-\udfff]', 
	        '\ud83d[\udc00-\ude4f]', 
	        '\ud83d[\ude80-\udeff]'
	    ];
	    str = str.replace(new RegExp(ranges.join('|'), 'g'), '');
	    return str;
	}
	//APP 原生交互
	function interactive(obj){
		var title=$("#shareTitle").val();
		var imgUrl=$("#shareImgUrl").val();
		var shareInfo=$("#shareInfo").val();
		var gaodeLongitude=$("#gaodeLongitude").val();
		var gaodeLatitude=$("#gaodeLatitude").val();
		var isOpacity=$("#isOpacity").val();
		if(obj=="android"){
			//Android获取分享信息
			androidFunction.getSharMsg(isOpacity,title,shareInfo,imgUrl); 
			//判断是否登录
			$("#ysApp,.ysApptologing").on("click",function(){ 
				androidFunction.isLogin();
	        })
			//点评页面 判断是否登录及内容验证
	   		$("#ysAppAno").on("click",function(){
	   			androidFunction.dpLogin();
	   		})
			//Android导航
			$("#appDaohang").on("click",function(){
				androidFunction.getLatLng(gaodeLatitude,gaodeLongitude);
			})
		   	//扫一扫
	   		$(".ysScan").on("touchstart",function(){
	   			androidFunction.scan();
	   		})
	   		//返回按钮
	   		$(".ysBack").on("touchstart",function(){
	   			androidFunction.back();
	   		})
	   		//支付
	   		$("#yg_pay").on("click",function(){
	   			var paymentType=$(".checked").attr("paymentType");
	   			var orderId=$("#orderId").val(); 
	   			var tradeNo=$("#tradeNo").val();
	   			var productName=$("#productName").val();
	   			var productDsecription=$("#productDsecription").val();
	   			var amount=$("#amount").val();
	   			var notifyURL=$("#notifyURL").val();
	   			var service=$("#service").val(); 
	   			var inputCharset=$("#inputCharset").val();
	   			var itBPay=$("#itBPay").val();
	   			var returnURL=$("#returnURL").val();
	   			var returnURLEncode=$("#returnURLEncode").val();
	   			var payforalipayEncodeStr=$("#payforalipayEncodeStr").val();
	   			payforalipayEncodeStr=decodeURIComponent(payforalipayEncodeStr);
	   			androidFunction.aliPay(returnURL,paymentType,orderId,tradeNo,productName,productDsecription,amount,notifyURL,service,inputCharset,itBPay,payforalipayEncodeStr);
	   		})
	   		//打印
	   		var tableNum=$("#tableNum").val(); 
			var yg_no=$(".yg_no").length;
			var mealNum=$("#mealNum").val();
			var orderTime=$("#orderTime").val();
			if(yg_no>0){
				var obj=[];
				for(var i=0;i<yg_no;i++){
					var title=$(".yg_title").eq(i).val();
					var price=$(".yg_price").eq(i).val();
					var no=$(".yg_no").eq(i).val();
					var category=$(".yg_categoryName").eq(i).val();
					obj[i]={};
					obj[i]["title"]=title;
					obj[i]["price"]=price;
					obj[i]["no"]=no;
					obj[i]["category"]=category;
				}	
				console.log(obj);
				androidFunction.print(tableNum,mealNum,orderTime,obj);
			}
  
		}else if(obj=="ios"){
			/*这段代码是固定的，必须要放到JS中*/
		    function setupWebViewJavascriptBridge(callback) {
		        if (window.WebViewJavascriptBridge) { return callback(WebViewJavascriptBridge); }
		        if (window.WVJBCallbacks) { return window.WVJBCallbacks.push(callback); }
		        window.WVJBCallbacks = [callback];
		        var WVJBIframe = document.createElement('iframe');
		        WVJBIframe.style.display = 'none';
		        WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__';
		        document.documentElement.appendChild(WVJBIframe);
		        setTimeout(function() { document.documentElement.removeChild(WVJBIframe) }, 0)
		    }
		  	/*与OC交互的所有JS方法都要放在此处注册，才能调用通过JS调用OC或者让OC调用这里的JS*/
			setupWebViewJavascriptBridge(function(bridge) {
				//导航栏是否透明
				bridge.callHandler('JSData', {'type':'opc','nav':isOpacity}, function(response) {
		     		console.log(response); 
		        })
		        //iOS原生登出
				bridge.registerHandler('logOut', function(data, responseCallback) {
		   			var logout="";
					document.cookie="JUSESSIONID="+logout+";path=/";
		     		responseCallback({'type':'opc','nav': "2121"});
		   		})
		   		//发送分享的数据
		   		var title=$("#shareTitle").val();
		   		bridge.callHandler('JSData', {'type':'shareMsg','title':title,'shareInfo':shareInfo,'imgUrl':imgUrl}, function(response) {
		   			console.log(response);
		        })
		        //询问原生是否登录
		   		$("#ysApp,.ysApptologing").on("click",function(e){
		   			bridge.callHandler('login', {'type':'login'}, function(response) {
			 			var date=new Date(); 
			   			date.setTime(date.getTime()+30*60*100000000);
						document.cookie="JUSESSIONID="+response.sesskey+";path=/;expires="+date.toGMTString();
						$("#yg_form").submit();
		   			})
		        })
		        //iOS点评页面 判断是否登录及内容验证
		   		$("#ysAppAno").on("click",function(e){
		   			bridge.callHandler('login', {'type':'login'}, function(response) {
		   	 			var date=new Date(); 
		   	   			date.setTime(date.getTime()+30*60*100000000);
		   				document.cookie="JUSESSIONID="+response.sesskey+";path=/;expires="+date.toGMTString();
		   				var str = $('.write').val().replace(/(^\s+)|(\s+$)/g,'');
		   	    		if(($('.light').length > 0)&&($('.write').val().replace(/(^\s+)|(\s+$)/g,'')!="")){
		   	    			$('.write').val(filteremoji(str));
		   	    			$('#postForm').submit();
		   	    		}else{
		   	    			$('.tishi').animate({'opacity':'0.5'},1500,function(){
		   	    				$('.tishi').animate({
		   	    					'opacity':'0'
		   	    				},1500);
		   	    			});
		   	    		}
		   	 		})
		   		})
		   		//导航
		   		$("#appDaohang").on("click",function(e){
		   			bridge.callHandler('JScallOC', {'type':'navigation','gaodeLatitude':gaodeLatitude,'gaodeLongitude':gaodeLongitude}, function(response) {
		   	 			console.log("gaodeLongitude------------->"+gaodeLongitude);
		   				console.log("gaodeLatitude------------->"+gaodeLatitude);	
		   	 			console.log(response);
		   	        })
		   		})
		   		//扫一扫
		   		$(".ysScan").on("touchstart",function(e){
		   			bridge.callHandler('scan', {'type':'scan'}, function(response) {
		   	 			console.log(response);
		   	        })
		   		})
		   		//返回按钮
		   		$(".ysBack").on("touchstart",function(e){
		   			bridge.callHandler('back', {'type':'back'}, function(response) {
		   	 			console.log(response);
		   	        })
		   		})
		   		//支付
		   		$("#yg_pay").on("click",function(e){
		   			var paymentType=$(".checked").attr("paymentType");
		   			var orderId=$("#orderId").val();
		   			var tradeNo=$("#tradeNo").val();
		   			var productName=$("#productName").val();
		   			var productDsecription=$("#productDsecription").val();
		   			var amount=$("#amount").val();
		   			var notifyURL=$("#notifyURL").val();
		   			var service=$("#service").val();
		   			var inputCharset=$("#inputCharset").val();
		   			var itBPay=$("#itBPay").val();
		   			var returnURL=$("#returnURL").val();
		   			var returnURLEncode=$("#returnURLEncode").val();
		   			var payforalipayEncodeStr=$("#payforalipayEncodeStr").val();
		   			payforalipayEncodeStr=decodeURIComponent(payforalipayEncodeStr);
		   			bridge.callHandler('alipay', {'type':'alipay','returnURL': returnURL,'returnURLEncode': returnURLEncode,'paymentType': paymentType, 'orderId': orderId,'tradeNo':tradeNo, 'productName': productName, 'productDsecription': productDsecription, 'amount': amount, 'notifyURL': notifyURL, 'service': service,'inputCharset': inputCharset, 'itBPay': itBPay,'payforalipayEncodeStr':payforalipayEncodeStr}, function(response) {
		   				console.log(response);
		   		    })
		   		})
		   		//打印
		   		var tableNum=$("#tableNum").val(); 
				var yg_no=$(".yg_no").length;
				var mealNum=$("#mealNum").val();
				var orderTime=$("#orderTime").val();
				if(yg_no>0){
					var obj=[];
					for(var i=0;i<yg_no;i++){
						var title=$(".yg_title").eq(i).val();
						var price=$(".yg_price").eq(i).val();
						var no=$(".yg_no").eq(i).val();
						var category=$(".yg_categoryName").eq(i).val();
						obj[i]={};
						obj[i]["title"]=title;
						obj[i]["price"]=price;
						obj[i]["no"]=no;
						obj[i]["category"]=category;
					}	
					console.log(obj);
					bridge.callHandler('print', {'type':'print','tableNum':tableNum,'mealNum':mealNum,'orderTime':orderTime,'data':obj}, function(response) {
		     			console.log(response);
		            })
				}
		 	})
		}
	}
	
})

//获取网页链接地址  name:url地址的参数
function GetQueryString(name){
		var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
		var r = window.location.search.substr(1).match(reg);
		if(r!=null)return  unescape(r[2]); return null;
}
//读取cookie name:cookie名
function getCookie(name){
	var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
	if(arr=document.cookie.match(reg)){
		return unescape(arr[2]);
	}else{return null;}
}


	
//原生登出
function androidLogout(){
	var logout="";
	document.cookie="JUSESSIONID="+logout+";path=/";
}
//判断是否登录
function isLogin(sesskey){
	var date=new Date(); 
	date.setTime(date.getTime()+30*60*100000000);
	document.cookie="JUSESSIONID="+sesskey+";path=/";
	$("#yg_form").submit();
}
//点评页面 判断是否登录及内容验证
function dpLogin(sesskey){
	var date=new Date(); 
	date.setTime(date.getTime()+30*60*100000000);
	document.cookie="JUSESSIONID="+sesskey+";path=/;expires="+date.toGMTString();
	var str = $('.write').val().replace(/(^\s+)|(\s+$)/g,'');
	if(($('.light').length > 0)&&($('.write').val().replace(/(^\s+)|(\s+$)/g,'')!="")){
		$('.write').val(filteremoji(str));
		$('#postForm').submit();
	}else{
		$('.tishi').animate({'opacity':'0.5'},1500,function(){
			$('.tishi').animate({
				'opacity':'0'
			},1500);
		});
	}
}

